/**
 * 
 */
package com.sip.comserver.communication;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sip.comserver.communication.server.InternalCommunicationServer;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * @author Dhanushka Ariyasingha.
 */
public abstract class JobListner {

    private  List<ConnectionThread> connectionList = null;
    
    private List<ConnectionThread> processedList =new ArrayList<ConnectionThread>();
    
    public JobListner() throws IOException {
        
        activateJobListner();
    }
    
    /**
     * Pick a passed thread to start and complete process. Once job is completed join with main thread.
     * 
     * @param connection
     * @throws InterruptedException
     */
    public void pick(final ConnectionThread connection) throws InterruptedException {

        System.out.println("Connection starting .. ");
        connection.start();

        connection.join();

    }

    protected void activateJobListner() throws IOException {
        
        final Thread jobThread =new Thread(new JobTread());
        
        jobThread.start();
        

    }
    
    public synchronized Boolean hasJob() {

        if (null != connectionList) {
            synchronized (connectionList) {

                return connectionList != null && connectionList.size() > 0;
            }
        }

        return Boolean.FALSE;
    }

    private class JobTread implements Runnable {

        public void run() {

            while (true) {

                try {
                    
                    connectionList = InternalCommunicationServer.getInstance().getConnectionList();
                    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

    }
    
    public CommunicationMessage recieveMessage(final ConnectionThread connectionThread) throws Exception {

        final CommunicationMessage message = connectionThread.read();

        final String header = message.getMessageHeader();
        final String Id = CommonUtils.find(header, "ID");
        System.out.println("Recieving message from clent : "+Id+" and passing to process.");

        return message;
    }
    
    /**
     * Send back to message and connected add to garbage buck to shutdown. 
     * 
     * @param connectionThread
     * @param message
     * @throws Exception
     */
    public void sendMessage(final ConnectionThread connectionThread, final CommunicationMessage message) throws Exception {
        
        System.out.println("Result sent back to client !");                    
        connectionThread.write(message);

        System.out.println("closing all connection !");
        
        addToGarbage(connectionThread);
    }
    
    public void removeConnection(final ConnectionThread connection) throws IOException{
        
        InternalCommunicationServer.getInstance().removeConnection(connection);
    }

    
    /**
     * @return the connectionList
     */
    protected List<ConnectionThread> getConnectionList() {

        return connectionList;
    }

    
    /**
     * @param connectionList the connectionList to set
     */
    protected void setConnectionList(final List<ConnectionThread> connectionList) {
    
        this.connectionList = connectionList;
    }
    
    protected void addToGarbage(final ConnectionThread connection) {

        processedList.add(connection);
    }
    
    /**
     * @param connectionList the connectionList to set
     * @throws IOException 
     */
    protected void clear() throws IOException {
    
        InternalCommunicationServer.getInstance().clear(processedList);
    }

}
